package com.singlemountain.gcommerce.web;


import com.singlemountain.gcommerce.SysParameters;
import com.singlemountain.gcommerce.domain.base.Base;
import com.singlemountain.gcommerce.domain.user.Userd;
import com.singlemountain.gcommerce.service.address.AddressdService;
import com.singlemountain.gcommerce.service.product.ProductdService;
import com.singlemountain.gcommerce.service.user.UserdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class BaseController<T extends Base, V extends BaseValidators> {

    //进入action方法的路径
    public final String URL_LOGIN="login";   //跳转到登录页面
    public final String URL_MY_ACCOUNT="/myAccount"; //跳转到个人信息

    //返回到jsp页面路径
    public final String VIEW_LOGIN="view_login";    //来到登录页面
    public final String VIEW_REDIRECT = "redirect:";   //来到注册页面
    public final String VIEW_MY_ACCOUNT = "view_my_account";   //来到我的信息页面


    @Autowired
    public UserdService userdService;
    @Autowired
    public AddressdService addressdService;

    @Autowired
    public ProductdService productdService;

    protected V getValidator() {
        return null;  //To change body of created methods use File | Settings | File Templates.
    }

    @InitBinder
    public void initBinder(WebDataBinder binder, HttpServletRequest request, ServletRequestDataBinder servletBinder) {
        if (getValidator() != null) {
            binder.setValidator(getValidator());
        }
        //CustomDateEditor dateEditor = new CustomDateEditor();
        //servletBinder.registerCustomEditor(Date.class, dateEditor);
        // binder.registerCustomEditor(Date.class, dateEditor);
    }

    public Userd getLoginUser(HttpSession session) {
        Userd userd = (Userd) session.getAttribute(SysParameters.SESSION_USER);
        userd = Userd.findUserd(userd.getId());
        return userd;
    }
}
